﻿
<html>

<head>

    <title>Updating a Google Spreadsheet with the Google data javascript client API</title>

    <script type="text/javascript" src="http://www.google.com/jsapi"></script>

    <script src="../../Scripts/jquery-1.7.1.js" type="text/javascript"></script>

    <script type="text/javascript">


        var resultsSheetName = "jeffarthur.co.uk - Javascript to Google Spreadsheet API example";
        scope = 'https://spreadsheets.google.com/feeds/ https://docs.google.com/feeds/';

        // Load the Google Data JS Client 
        google.load('gdata', '2.0');

        function logMeIn(scope) {
            var token = google.accounts.user.login(scope);
            alert("You Should Now Be Logged In");
        }
         
        function handleInfo(data) {
            var response = eval(data.currentTarget.responseText);
            alert('Target: ' + response.Target + "\n" +
		  'Scope: ' + response.Scope + "\n" +
		  'Secure: ' + response.Secure);
        }

        function doGetInfo(scope) {
            if (google.accounts.user.checkLogin(scope)) {
                google.accounts.user.getInfo(handleInfo);
            }
        }


        function checkOAuth(scope) {
            var token = google.accounts.user.checkLogin(scope);
            alert(token);
        }

        function logMeOut() {
            google.accounts.user.logout();
        }

        function getResults() {
            var scope = 'https://docs.google.com/feeds/';
            if (google.accounts.user.checkLogin(scope)) {
                var service = new google.gdata.client.GoogleService('writely', 'DocList-App-v3.0');
                var feedurl = 'https://docs.google.com/feeds/default/private/full/-/spreadsheet/?v=3';
                service.getFeed(feedurl, handleFeed, handleError);
            }
        }

        var handleFeed = function (response) {
            var entries = response.feed.entry;
            if (!response.feed.entry) {
                createSheet();
                return;
            }
            for (var i = 0, entry; entry = entries[i]; i++) {
                var title = entry.title.$t;
                if (title == resultsSheetName) {
                    alert("we have our sheet! ");
                    var dummy = entry.gd$resourceId.$t.split(':');
                    resultSheetID = dummy[1];
                    initTopRow();
                }
            }
        };

        /**

        * Error handler that gets called if there was an error loading the feed.

        * The error message is displayed on the page.

        */
        function errorHandler(response) {
            alert(response.message);
        }

        function handleError(e) {
            alert("There was an error!");
            alert(e.cause ? e.cause.statusText : e.message);
        }

        function createSheet() {
            var scope = 'https://docs.google.com/feeds/';
            if (google.accounts.user.checkLogin(scope)) {
                var service = new google.gdata.client.GoogleService('writely', 'DocList-App-v3.0');
                var entry = new google.gdata.Entry();
                var category = new google.gdata.atom.Category({
                    label: 'spreadsheet',
                    scheme: 'http://schemas.google.com/g/2005#kind',
                    term: 'http://schemas.google.com/docs/2007#spreadsheet'
                });
                entry.addCategory(category);
                var title = new google.gdata.atom.Text();
                title.setText(resultsSheetName);
                title.setType('text');
                entry.setTitle(title);
                service.insertEntry(scope + 'documents/private/full/', entry, handleEntry, handleError);
            }
        }
        var handleEntry = function (entryRoot) {
            alert('Spreadsheet: ' + resultsSheetName + ' Created');
        }

        var handleError = function (e) {
            alert('Error: ' + e.cause ? e.cause.statusText : e.message);
        }

        function initTopRow() {
            var scope = 'https://spreadsheets.google.com/feeds/';
            if (google.accounts.user.checkLogin(scope)) {
                var service = new google.gdata.client.GoogleService('wise', 'SpreadSheet-App-v3.0');
                var entry = new google.gdata.Entry();
                entry.xmlns$gs = 'http://schemas.google.com/spreadsheets/2006';
                var rcURI = encodeURI(scope + 'cells/' + resultSheetID + '/1/private/full/R1C1');

                var id = new google.gdata.ExtendedProperty();
                id.$t = rcURI;
                entry.id = id;

                var link = new google.gdata.Link();
                link.href = rcURI;
                link.rel = 'edit'
                link.type = 'application/atom+xml'
                entry.link = link;

                var gs$cell = new google.gdata.ExtendedProperty();
                gs$cell.row = '1';
                gs$cell.col = '1';
                gs$cell.inputValue = 'title';
                entry.gs$cell = gs$cell;
                var headers = new Object;
                headers['If-Match'] = '*';
                service.setHeaders(headers);
                service.updateEntry(rcURI, entry, handleColHeadings, handleError);
            }

        }
        var handleColHeadings = function (entryRoot) {
            alert('Heading row created ');
        }

        function appendRow() {
            var scope = 'https://spreadsheets.google.com/feeds/';
            if (google.accounts.user.checkLogin(scope)) {
                var service = new google.gdata.client.GoogleService('wise', 'SpreadSheet-App-v3.0');
                var entry = new google.gdata.Entry();
                entry.xmlns$gsx = 'http://schemas.google.com/spreadsheets/2006/extended';
                var SheetURI = encodeURI(scope + 'list/' + resultSheetID + '/1/private/full');

                var currentTime = new Date()
                var hours = currentTime.getHours()
                var minutes = currentTime.getMinutes()
                var seconds = currentTime.getSeconds()

                if (minutes < 10) {
                    minutes = "0" + minutes;
                }
                if (seconds < 10) {
                    seconds = "0" + seconds;
                }

                ent = new google.gdata.ExtendedProperty();
                ent.$t = hours + ':' + minutes + ':' + seconds;
                entry.gsx$title = ent;
                service.insertEntry(SheetURI, entry, handleRowEntry, handleError);
            }
        }
        var handleRowEntry = function (entryRoot) {
            alert('Row Added');
        }





    </script>

  </head>

  <body>

    <h2>Example: Updating  A Google Spreadsheet with Javascript</h2>

    <button onclick="logMeIn(scope);">login</button>

    <button onclick="checkOAuth(scope);">check login</button>

    <button onclick="doGetInfo(scope);">debug login</button>

    <button onclick="createSheet(scope);">Create Sheet</button>

    <button onclick="getResults(scope);">Look For Sheet</button>

    <button onclick="appendRow(scope);">Append Row</button>

    <button onclick="logMeOut();">logout</button>

    <img src="/images/construction.gif">

	<div id="data1"><!-- dynamically filled --></div>

	

  </body>



</html>





